package com.cityguardian.threads;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.Toast;

import com.cityguardian.cityguardianapp.MapActivity;
import com.cityguardian.model.WrapperAlerta;
import com.cityguardian.ws.ConnectionThread;
import com.google.gson.Gson;

public class AsyncTaskAlerta extends AsyncTask<WrapperAlerta, Double, String> {
	public final static String EXTRA_MESSAGE = "com.cityguardian.cityguardian.mainMessage";	
	private ProgressDialog dialog;
	private ActionBarActivity activity;
	private WrapperAlerta wrapperAlerta;
	
	public AsyncTaskAlerta(ActionBarActivity activ){
		this.activity=activ;
		this.dialog = new ProgressDialog(activity);
	}
	
    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Enviando Alerta");
        this.dialog.show();
    }
    
	@Override
	protected String doInBackground(WrapperAlerta... params) {
		wrapperAlerta=params[0];
		String response=registarAlerta(params[0]);
		return response;
	}
	
	@Override
	protected void onPostExecute(String result) {
		
		if (this.dialog.isShowing()) {
			this.dialog.dismiss();
		}
		if (result== null){
			Toast.makeText(activity, 
					"Intenta de nuevo por favor", Toast.LENGTH_LONG).show();
		}else{
				    	
    		AsyncTaskSubirFoto thread = new AsyncTaskSubirFoto(wrapperAlerta.getAlerta().getFoto(),result);
    		thread.execute();			
			
			Toast.makeText(activity, 
					"Se registro la alerta, favor de esperar la respuesta", Toast.LENGTH_LONG).show();
	    	Intent intent = new Intent(activity, MapActivity.class);
	    	intent.putExtra("Usuario", wrapperAlerta.getUsuario());
	    	activity.startActivity(intent); 
		}    	
	}

	private String registarAlerta(WrapperAlerta wrapperAlerta) {
		List<NameValuePair> param = new ArrayList<NameValuePair>();
		param.add(new BasicNameValuePair("posicionX", wrapperAlerta.getAlerta().getPosicionX().toString()));
		param.add(new BasicNameValuePair("posicionY", wrapperAlerta.getAlerta().getPosicionY().toString()));
		param.add(new BasicNameValuePair("descripcion", wrapperAlerta.getAlerta().getDescripcion()));
		param.add(new BasicNameValuePair("idUsuario", wrapperAlerta.getUsuario().getIdUsuario()));
		param.add(new BasicNameValuePair("idTipoAlerta", wrapperAlerta.getAlerta().getNombreTipoAlerta()));
		param.add(new BasicNameValuePair("fecha", wrapperAlerta.getAlerta().getFecha()));
		param.add(new BasicNameValuePair("ciudad", wrapperAlerta.getAlerta().getCiudad()));		
		
		String route = "/ws/alerta/set_alert_all/";
		Log.e("log_tag",param.toString());
		
		ConnectionThread connection = new ConnectionThread(param, route);
		String responseBody = connection.getResponse();
		
		if (responseBody==null){
			Log.e("log_tag", "Error in webservice");
			return null;
		}
		//TODO ARREGLAR
	    Gson gson = new Gson();
	    Log.e("log_tag", responseBody);
		String idAlerta = gson.fromJson(responseBody,String.class);
		
		return idAlerta;
	}

}
